package com.dragon.modules.yuegou.service.impl.live;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.client.wechat_mini.common.MiniConfig;
import com.dragon.modules.yuegou.client.wechat_mini.request.RoomAddgoodsRequest;
import com.dragon.modules.yuegou.client.wechat_mini.service.MiniService;
import com.dragon.modules.yuegou.client.wechat_mini.service.impl.MiniServiceImpl;
import com.dragon.modules.yuegou.entity.live.BroadcastSpuEntity;
import com.dragon.modules.yuegou.entity.live.ReviewSpuEntity;
import com.dragon.modules.yuegou.enums.code.LiveCode;
import com.dragon.modules.yuegou.mapper.live.BroadcastSpuMapper;
import com.dragon.modules.yuegou.model.live.Broadcast;
import com.dragon.modules.yuegou.model.live.BroadcastSpu;
import com.dragon.modules.yuegou.model.system.Application;
import com.dragon.modules.yuegou.query.live.BroadcastSpuQuery;
import com.dragon.modules.yuegou.query.live.ReviewSpuQuery;
import com.dragon.modules.yuegou.service.live.BroadcastService;
import com.dragon.modules.yuegou.service.live.BroadcastSpuService;
import com.dragon.modules.yuegou.service.live.ReviewSpuService;
import com.dragon.modules.yuegou.service.system.ApplicationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品直播间关系表 服务实现
 *
 * @author ZSH
 * @date 2020-06-05 19:52:37
 * @version 2.0
 */
@Service("liveBroadcastSpuService")
public class BroadcastSpuServiceImpl
		extends ServiceImpl<BroadcastSpuMapper, BroadcastSpuEntity>
		implements BroadcastSpuService {

	@Autowired
	private ApplicationService applicationService;
	
	@Autowired
	private ReviewSpuService reviewSpuService;

	@Autowired
	private BroadcastService broadcastService;

	@Override
	@Transactional
	public void goodsLiveRoom(String broadcastId, List<String> reviewIds,String busdId) {
		if (CollectionUtils.isEmpty(reviewIds)) {
			return;
		}
		Broadcast broadcast = broadcastService.broadDetails(broadcastId);
		if (broadcast==null || StringUtils.isBlank(broadcast.getRoomId())){
			throw new DragonException(LiveCode.ROOM_NOT_EXIST);
		}

		ReviewSpuQuery query = new ReviewSpuQuery();
		query.id().in(reviewIds);
		List<ReviewSpuEntity> list = reviewSpuService.list(query.toQueryWrapper());
		
		//判断商品是否已经进入直播间
		for (ReviewSpuEntity reviewSpu : list) {
			BroadcastSpuQuery broadcastSpuQuery = new BroadcastSpuQuery();
			broadcastSpuQuery.setReviewId(reviewSpu.getId());
			broadcastSpuQuery.setBroadcastId(broadcastId);
			BroadcastSpuEntity one = getOne(broadcastSpuQuery.toQueryWrapper());
			if (one != null) {
				throw new DragonException(LiveCode.LIVE_ERROR.getCode(),String.format(LiveCode.LIVE_ERROR.getMessage(),"商品"+reviewSpu.getName()+"已经关联直播间"));
			}
		}
		Application application = applicationService.getOriginalId(busdId);
		MiniService service = new MiniServiceImpl(new MiniConfig(application.getAppid(), application.getAppsecret()));
		List<Integer> goodIds = list.stream().map(spu -> Integer.parseInt(spu.getGoodsId())).collect(Collectors.toList());
		RoomAddgoodsRequest request = new RoomAddgoodsRequest();
		request.setIds(goodIds);
		request.setRoomId(Integer.parseInt(broadcast.getRoomId()));
		//插入直播商品
		service.live().roomAddgoods(request);
		//添加房间和审核商品的关系
		for (ReviewSpuEntity reviewSpu : list) {
			BroadcastSpu broadcastSpu = new BroadcastSpu();
			broadcastSpu.setReviewId(reviewSpu.getId());
			broadcastSpu.setBroadcastId(broadcastId);
			broadcastSpu.setRoomId(broadcast.getRoomId());
			baseMapper.insert(broadcastSpu);
		}
	}
	
}